//
//  DownLoadData.m
//  EarnBao
//
//  Created by xiaohui on 15/4/24.
//  Copyright (c) 2015年 杭州赚宝科技有限公司. All rights reserved.
//

#import "DownLoadData.h"
#import "AFNetworking.h"

@implementation DownLoadData

//声明一个静态对象成为单例
static DownLoadData *handle = nil;

//实现单例的类方法，全局只有一个单例对象
+ (DownLoadData *)shareDataHandle
{
    if (handle == nil) {
        
        handle = [[DownLoadData alloc] init];
    }
    return handle;
}

//接口数据的下载解析
- (void)downLoadDataWithURL:(NSString *)urlString key:(NSString *)keyName model:(NSString *)modelName results:(NSMutableArray *)dataArray
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *sourceDic = (NSDictionary *)responseObject;
        
        NSArray *sourceArr = [sourceDic objectForKey:keyName];
        
        for (NSDictionary *dic in sourceArr) {
            
            Class myClass = NSClassFromString(modelName);
            id sender = [[myClass alloc] init];
            [sender setValuesForKeysWithDictionary:dic];
            [dataArray addObject:sender];
        }
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(refresh)]) {
            
            [self.delegate refresh];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        //NSLog(@"%@",error);
    }];
}

@end
